/**
* 公共库
* date 2012-12-13
*/

/*对话框*/
var linji_dialog={
	setting:{
		obj:null,
		dialogClass:'linji_dialog',
		mask:'',
		maskClass:'linji_mask',
		windowWidth:$(window).width(),
		windowHeight:$(window).height(),
		objWidth:"",	
		title:'对话框',
		isDrag:false,
		html:''
	},
	init:function(param){
		var that=this;
		that.setting=$.extend({},that.setting,param); 
		that.setting.obj=$(that.template);
		$('.'+that.setting.dialogClass).remove();
		$('body').append(that.setting.obj);		
		that.lock();
		that.getElement();
		that.bindEvent();
		that.addHtmlToContent();
	},
	setTitle:function(str){
		this.setting.dialogTitle.html(str);
	},
	addHtmlToContent:function(){
		this.setting.dialogContent.html(this.setting.html);
	},	
	lock:function(){
		$('body').addClass('linji_body');
		var height=$('body').height() + 'px';
		if($.browser.version=='6.0')
		{
			this.setting.mask=$('<iframe class="'+this.setting.maskClass+'" src="about:blank" style="width:100%;height:100%;position:absolute;top:0;left:0;filter:alpha(opacity=0)"></iframe>');	
		}else
			{
				this.setting.mask=$('<div class="'+this.setting.maskClass+'"></div>');				
			}
		$('.'+this.setting.maskClass).remove();
		$('body').append(this.setting.mask);			
	},
	getElement:function(){
		this.setting.closeBtn=this.setting.obj.find('#linji_dialog_close_btn');
		this.setting.dialogTitle=this.setting.obj.find('#linji_dialog_title_value');
		this.setting.dialogContent=this.setting.obj.find('#linji_dialog_content_value');
		this.setting.dialogTitle.html(this.setting.title);
	},	
	position:function(){
		var that=this;
		var currentLeft=(that.setting.windowWidth - that.setting.obj.outerWidth()) / 2.0;
		var currentTop=(that.setting.windowHeight*0.15+$(document).scrollTop()) /6;
		var objWidth=that.setting.objWidth;
		if(objWidth=='')objWidth=that.setting.obj.innerWidth();
		that.setting.obj.css({
			'left':currentLeft+$(document).scrollLeft(),
			'top':currentTop+$(document).scrollTop(),
			'width':objWidth
		});
	},
	show:function(){
		this.position();
		this.setting.obj.fadeIn();
	},
	hide:function(){
		var that=this;
		this.setting.obj.fadeOut(function(){
			that.setting.mask.remove();
			that.setting.obj.remove();
		});
		$('body').removeClass('linji_body');
	},
	bindEvent:function(){
		var that=this;
		that.setting.closeBtn.on('click',function(){
			that.hide();
		});
		that.setting.mask.on('click',function(){
			that.hide();
		});
		that._scrollEvent();
		if(that.setting.isDrag)
		{
			that._dragEvent();		
		}
	},
	_scrollEvent:function(){
		var that=this,sTimer=null;
		var currentTop=(that.setting.windowHeight*0.15+$(document).scrollTop())/6;
		$(window).scroll(function(){
			that.setting.obj.css({
				'top':$(document).scrollTop()+currentTop
			});	
		});
	},
	_dragEvent:function(){
		var that=this;
		var mouse = {x:0,y:0};				
		function drag(event){
			var e = window.event || event;
			var top = parseInt(that.setting.obj.css('top')) + (e.clientY - mouse.y);
			var left = parseInt(that.setting.obj.css('left')) + (e.clientX - mouse.x);
			that.setting.obj.css({top:top,left:left});
		    mouse.x = e.clientX;
		    mouse.y = e.clientY;
		}
		that.setting.obj.find(".linji_dialog_title").mousedown(function(event){
			var e = window.event || event;		
		    mouse.x = e.clientX;
		    mouse.y = e.clientY;
		    $(document).bind('mousemove',drag);
		});
		$(document).mouseup(function(event){
		    $(document).unbind('mousemove',drag);
		});		
	},
	template:'<div class="linji_dialog"><table><tr><td class="linji_dialog_title"><h3 class="fl" id="linji_dialog_title_value">标题</h3><a href="javascript:void(0);" title="关闭" class="linji_dialog_close" id="linji_dialog_close_btn"></a></td></tr><tr><td class="linji_dialog_content" id="linji_dialog_content_value"></td></tr></table></div>'
};

/*布局特效*/
var layout={
	init:function(){
		this.nav();
		this.closeTipEvent();
	},
	//导航
	nav:function(){
		var $sidebar=$('.sidebar_nav');
		var $parent=$sidebar.find('dt');
		var $links=$sidebar.find('dd a');
		$parent.on('click',function(){
			var $this=$(this);
			$this.toggleClass('current');
			var $next=$this.next('dd');
			$next.toggle();
			$this.parent().siblings('.sidebar_nav').find('dt').removeClass('current');
			$this.parent().siblings('.sidebar_nav').find('dd').hide();
		});
		$links.hover(function(){
			$(this).addClass('current');
		},function(){$(this).removeClass('current');});
	},
	//全选
	listCheckAll:function(){
		var $checkAll=$('.check_all');
		$checkAll.on('click',function(){
			$("input[name='id[]']:checkbox").attr("checked", this.checked);
		});
		$("input[name='id[]']:checkbox").on('click',function(){
			var $tmp=$("input[name='id[]']:checkbox");
			$checkAll.attr('checked',$tmp.length==$tmp.filter(':checked').length);
		});
	},
	//操作选择
	listFormSelectType:function(formId,selectId,btnId){
		$('#'+btnId).on('click',function(){
			var type=$('#'+selectId).val();
			var $form=$('#'+formId);
			if($("input[name='id[]']:checkbox",$form).filter(':checked').length==0)
			{
				alert('请选择你要处理的信息');return false;
			}
			if(confirm("是否将进行此操作?"))
			{
				$form[0].action=type;
				$form[0].submit();			
			}
			return false;
		});		
	},
	//单项删除
	delDataConfirm:function(objClass,msg){
		msg=msg || "是否将进行删除操作?";
		objClass=objClass || "delHandle";
		$('.'+objClass).on('click',function(){
			if(confirm(msg))
			{
				return true;			
			}else{return false;}			
		});
	},
	//表格间隔色
	listTableColor:function(colorClass,odd,tableClass){
		var tableClass=tableClass || 'public_table';
		var colorClass=colorClass || 'alt_row';
		var odd=odd || 'even';
		var $tableTr=$('.'+tableClass+' tr:'+odd).addClass(colorClass);
	},
	//信息tip
	msgTip:function(title,typeClass,container){
		var containerId=container || 'mainContainer';
		var typeClass=typeClass || 'attention';
		var $container=$('#'+containerId);
		var html='<div class="public_notification '+typeClass+' png_bg"><a href="javascript:void(0);" class="close" title="关闭" ><img src="images/icons/cross_grey_small.png" alt="关闭" /></a><div>'+title+'</div></div>';
		var $parent=$(html);
		var $closeBtn=$parent.find('.close');
		$closeBtn.on('click',function(){
			var $parent=$(this).parent();
			$parent.fadeOut(function(){
				$(this).remove();
			});
		});
		$container.prepend($parent);
	},
	//关闭信息Tip事件绑定
	closeTipEvent:function(){
		$(".public_notification").delegate(".close","click",function(){
			var $parent=$(this).parent();
			$parent.fadeOut(function(){
				$(this).remove();
			});			
		});
	}	
};
